<?php 
	require_once("models/config.php");
	require_once("pie.php");

	require_once("graphs/graph-settings.php");
	
	if (!isUserViewAuthorized()) {
		echo "Unauthorized access";
		die();
	}
	
	$usedUser = $_GET['userId'];
	
	global $loggedInUser;
	global $db;

	$sql = "SELECT `root_Causes`.`SubcategoryId` AS `SubcategoryId`, `root_Cause_Subcategories`.`Name` AS `Name`, COUNT(*) AS Count 
		FROM `debugging_Reports` JOIN `root_Causes` ON `debugging_Reports`.`Root_Cause` = `root_Causes`.`Id` JOIN `root_Cause_Subcategories` ON `root_Causes`.`SubcategoryId` = `root_Cause_Subcategories`.`Id`

		WHERE (((`Review_Reason` IS NULL) OR (`Reviewed` = 1)) AND `debugging_Reports`.`User` = $usedUser)
		GROUP BY `root_Causes`.`SubcategoryId`";
	
	$result = $db->sql_query($sql);
	if (!$result) {
		$error = $db->sql_error();
		return;
	}
		
	$data = array();
	$names = array();
	$rows = $db->sql_fetchall();
	foreach($rows as $i => $row) {
		$data[] = $row['Count'];
		$names[] = $row['Name'];
	}

	drawPie($data, $names,
		GRAPH_WIDTH,
		GRAPH_HEIGHT,
		DEFAULT_PIE_RADIUS,
		DEFAULT_LABEL_LEFT,
		DEFAULT_PIE_TOP,
		DEFAULT_TITLE_BAR_HEIGHT,
		"Frequency of root causes");
?>
